<?php

namespace frontend\modules\business\controllers\user;

use common\components\Upload;
use common\models\business\BusinessPig;
use common\models\farmerhouse\FarmerhouseTravels;
use common\models\farmerhouse\FarmerhouseTravelsDetail;
use common\models\shop\ShopHouseOrder;
use common\models\shop\ShopUserAddress;
use frontend\controllers\BaseMobileController;
use yii\db\Query;
use yii\helpers\Url;


/**
 * 农家乐体验
 */
class FarmerExperienceController extends BaseMobileController
{


    /**
     * @return string
     * 农家体验
     */
    public function actionIndex()
    {
        $travels = (new Query())->select('t.title as content,t.id,td.picture as img')->from(FarmerhouseTravels::tableName().' as t')
            ->leftJoin(FarmerhouseTravelsDetail::tableName().' as td','td.travelid=t.id')
            ->where('t.userid=:uid',[':uid'=>$this->user['userid']])
            ->groupBy('t.id')
            ->all();
        foreach ($travels as $key => $row)
        {
            $travels[$key]['url'] = Url::to(['/business/user/farmer-experience/detail','id'=>$row['id']]);
            $travels[$key]['img'] = Upload::checkGetUrl($row['img'],false,'img');
        }


        return $this->render('@template/mobile/farm_experience.html',[
            'travels' => \GuzzleHttp\json_encode($travels)
        ]);
    }


    /**
     * @param $hid
     * @return string
     * 编辑游记页
     */
    public function actionEdit($hid)
    {

        return $this->render('@template/mobile/experience_edit.html',[

        ]);

    }


    public function actionDetail($id)
    {
        $travels = (new Query())->select('*')->from(FarmerhouseTravels::tableName().' as t')
            ->where('id=:id',[':id'=>$id])
            ->one();

        $details = (new Query())->select('*,picture as imgs')->from(FarmerhouseTravelsDetail::tableName().' as td')
            ->where('travelid=:id',[':id'=>$id])
            ->all();
        foreach ($details as $key => $row)
        {
            $details[$key]['imgs'] = Upload::checkGetUrl($row['imgs'],true,'imgs',false);
        }
        $travels['data'][] = $details;
//        echo \GuzzleHttp\json_encode($travels);exit;

        return $this->render('@template/mobile/experience_detail.html',[
            'detail' => \GuzzleHttp\json_encode($travels)
        ]);

    }



    /**
     * @return array
     * 新增游记
     */
    public function actionAddTravel()
    {
        $postdata = \Yii::$app->request->post();
        $data = [
            'userid' => $this->user['userid'],
            'houseid' => $postdata['hid'],
            'title' => $postdata['title'],
            'status' => 0,
            'createtime' => time()
        ];
        $res = \Yii::$app->db->createCommand()->insert(FarmerhouseTravels::tableName(),$data)->execute();
        $travelid = \Yii::$app->db->getLastInsertID();

        foreach($postdata['info'] as $key => $row)
        {
            if(is_array($row) && !empty($row)){
                foreach ($row as $k => $r){
                    if(array_key_exists('imgs',$postdata) && !empty($postdata['imgs'][$key][$k])){
                        if(count($postdata['imgs'][$key][$k]) >= 2){
                            $imgs = implode('%^',$postdata['imgs'][$key][$k]);
                            $imgs = implode('|',Upload::uploadBase64($imgs));
                        }else{
                            $imgs = $postdata['imgs'][$key][$k][0];
                            $imgs = Upload::uploadBase64($imgs);
                        }
                    }else{
                        $imgs = '';
                    }
                    $data = [
                        'travelid' => $travelid,
                        'station' => $k+1,
                        'day' => $key+1,
                        'picture' => $imgs,
                        'content' => $r,
                    ];
                    $res = \Yii::$app->db->createCommand()->insert(FarmerhouseTravelsDetail::tableName(),$data)->execute();
                }
            }
        }

        //更新订单的书写游记状态
        \Yii::$app->db->createCommand()->update(ShopHouseOrder::tableName(),['iftravel'=>1],'houseid=:hid and userid=:uid and id=:id',[':hid'=>$postdata['hid'],':uid'=>$this->user['userid'],':id'=>$postdata['oid']])
            ->execute();
        return $res?$this->jsonMsg('提交成功',0,['url'=>Url::to(['/business/user/farmer-experience/index'])]):$this->jsonMsg('提交失败','1');
    }




}